/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PLLIST_H
#define PLLIST_H

#include <tools/PLIListIterator.h>
#include <tools/PLListIterator.h>
#include <tools/PLLinkedListNode.h>

namespace PLSDK {
    
    /**
     * A simple list.
     */
    template<typename T>
    class PLList : public PLIListIterator<T> {
    public:
        /**
         * Constructor.
         */
        inline PLList() {
            m_first = new PLListNode<T>();
        }
        /**
         * Destructor.
         */
        ~PLList();
        /**
         * Remove all elements from the list.
         */
        inline void clear();
        /**
         * Add a new object to the current position.
         *
         * @param object Object to add.
         * @param current The current node of the list.
         */
        inline virtual void add(T object, PLListNode<T> *current = 0);
        /**
         * Append a new object.
         *
         * @param object Object to append.
         */
        inline virtual void append(T object) {
            add(object);
        }
        /**
         * Contains the list a object.
         *
         * @param object Object to test for.
         * @return True if the list contains the object.
         */
        inline bool contains(T object);
        /**
         * The number of objects in the list.
         *
         * @return The number of objects in the list.
         */
        inline pluint64 count();
        /**
         * Get a new iterator for the list.
         *
         * @return The iterator for the list.
         */
        inline PLListIterator<T> iterator() {
            return PLListIterator<T>(this, m_first);
        }
        /**
         * Have the current node a next object.
         *
         * @param current The current node.
         * @return true If the node have a next node.
         */
        inline bool hasNext(PLListNode<T> *current);
        /**
         * Have the current node a previous node.
         *
         * @param current The current node.
         * @return true If the node have a previous node.
         */
        inline bool hasPrevious(PLListNode<T> *current);
        /**
         * Get the next object.
         *
         * @param current The current node.
         * @return The object and go to the next object.
         */
        inline T next(PLListNode<T> *current);
        /**
         * Get the previous object.
         *
         * @param current The current node.
         * @return The object and go to the previous object.
         */
        inline T previous(PLListNode<T> *current);
        /**
         * Get the object for a position in the list.
         *
         * @param position The position to get the element from.
         * @return The value of the position in the list.
         */
        inline virtual T at(pluint64 position);
        /**
         * Delete a object.
         *
         * @param current The object to remove.
         * @return The new current node.
         */
        inline PLListNode<T> *remove(PLListNode<T> *current);
        /**
         * Delete a object.
         *
         * @param object Object to delete from the list.
         * @return True if the object was deleted from the list.
         * If no object exists it returns false.
         */
        inline virtual plbool removeAll(T object);
        /**
         * Set a object to the current object.
         *
         * @param object The data to set.
         * @param current The current node.
         */
        inline void set(T object, PLListNode<T> *current = 0);
        /**
         * Get the hash value of the object.
         *
         * @return The hash value of the object. The hash for the PLObject object was 1.
         */
        inline virtual pluint hash() const {
            return 2;
        }
        /**
         * Compare to objects.
         *
         * @param object Object to compare with.
         * @return The result of the compare.
         * @todo implement
         */
        inline virtual PLCompareResult compareTo(PLObject *object);
        /**
         * Get the content of the first element in the list.
         *
         * @return The content of the first element.
         */
        inline const T first() {
            return firstNode().content();
        }
        /**
         * Get the content of the last element in the list.
         *
         * @return The content of the last element.
         */
        inline virtual const T last() {
            return lastNode().content();
        }
        /**
         * Get the first node of the list.
         *
         * @return The first node.
         */
        inline PLListNode<T> &firstNode() {
            return *m_first->next();
        }
        /**
         * Get the last node of the list.
         *
         * @return The last node.
         */
        inline virtual PLListNode<T> &lastNode();
    protected:
        PLListNode<T> *m_first;
    private:
        PLDISABLE_COPY(PLList<T>)
    };

    template<typename T>
    PLList<T>::~PLList() {
        if(m_first != 0) {
            PLListIterator<T> it = iterator();
            while(it.hasNext()) {
                it.remove();
            }
            delete m_first;
            m_first = 0;
        }
    }

    template<typename T>
    void PLList<T>::clear() {
        PLListIterator<T> it = iterator();
        while(it.hasNext()) {
            it.remove();
        }
    }

    template<typename T>
    pluint64 PLList<T>::count() {
        pluint64 countValue = 0;
        PLListIterator<T> it = iterator();
        while(it != lastNode()) {
            countValue++;
            it.next();
        }
        return countValue;
    }

    template<typename T>
    void PLList<T>::add(T object, PLListNode<T> *current) {
        if(current == 0) {
            current = &lastNode();
        }
        PLListNode<T> *newNode = new PLListNode<T>(object);
        PLListNode<T> *b = 0;
        if(current->next() != 0) {
            b = current->next();
        }
        current->setNextNode(newNode);
        if(b != 0) {
            newNode->setNextNode(b);
        }
    }

    template<typename T>
    bool PLList<T>::contains(T object) {
        PLListNode<T> *tempNode = m_first;
        T value;
        while((tempNode = tempNode->next()) != 0) {
            value = tempNode->content();
            // TODO Compare the pointer better.
            if(( is_pointer<T>::value && value != 0 && value == object) ||
               (!is_pointer<T>::value && value == object)) {
                return true;
            }
        }
        return false;
    }

    template<typename T>
    bool PLList<T>::hasNext(PLListNode<T> *current) {
        if(current != 0) {
            return current->next() != 0;
        }
        return false;
    }

    template<typename T>
    bool PLList<T>::hasPrevious(PLListNode<T> *) {
        return false;
    }

    template<typename T>
    T PLList<T>::next(PLListNode<T> *current) {
        if(current != 0) {
            if(hasNext(current)) {
                return current->next()->content();
            }
        }
        return T();
    }

    template<typename T>
    T PLList<T>::previous(PLListNode<T> *) {
        return T();
    }

    template<typename T>
    T PLList<T>::at(pluint64 position) {
        PLListNode<T> *current = m_first;
        pluint64 pos = 0;
        while(current->next() != 0) {
            current = current->next();
            if(pos == position) {
                return current->content();
            }
            pos++;
        }
        return T();
    }

    template<typename T>
    PLListNode<T> *PLList<T>::remove(PLListNode<T> *current) {
        if(current != 0) {
            PLListNode<T> *tempNode = m_first;
            PLListNode<T> *ret;
            while(tempNode->next() != 0 && *tempNode->next() != *current) {
                tempNode = tempNode->next();
            }
            if(tempNode->next() != 0 && *tempNode->next() == *current) {
                ret = current->next();
                tempNode->setNextNode(ret);
                delete current;
                current = 0;
                return ret;
            }
        }
        return 0;
    }

    template<typename T>
    plbool PLList<T>::removeAll(T object) {
        PLListNode<T> *tempNode = m_first;
        PLListNode<T> *nextNode;
        plbool del = false;
        while(tempNode != 0) {
            if(tempNode->next() != 0 && tempNode->next()->content() == object) {
                nextNode = tempNode->next();
                tempNode->setNextNode(nextNode->next());
                delete nextNode;
                del = true;
            }
            tempNode = tempNode->next();
        }
        return del;
    }

    template<typename T>
    void PLList<T>::set(T object, PLListNode<T> *current) {
        if(current != 0) {
            current->setContent(object);
        }
    }

    template<typename T>
    PLListNode<T> &PLList<T>::lastNode() {
        PLListNode<T> *current = m_first;
        while(current != 0 && current->next() != 0) {
            current = current->next();
        }
        return *current;
    }

    template<typename T>
    PLCompareResult PLList<T>::compareTo(PLObject *object) {
        PLUNUSED(object)
        return PLCompareResultSame;
    }

}

#endif
